<?php

namespace app\index\model;

use think\Cache;
use think\Db;
use think\Model;

class KuaiDiUserModel extends Model {

    public function getUserInfo($userName, $pass)
    {
        $where = [
            'user_name' => $userName,
            'password' => md5($pass),
        ];
        $info = Db::table('kuaidi_user')->where($where)->find();
        if (empty($info)) {
            $res['success'] = false;
            $res['desc'] = '密码错误';
            $res['errorcode'] = '200001';
            return $res;
        }

        $access_token = md5(uniqid() . mt_rand());
        $info['token'] = $access_token;
        if(empty($info['avatar'])) {
            $info['avatar'] = get_http_type() . $_SERVER['HTTP_HOST'] . "/image/icon.jpeg";
        } else {
            $info['avatar'] = get_http_type() . $_SERVER['HTTP_HOST'] . $info['avatar'];
        }
        Cache::set($access_token, $info, 86400 * 10);
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $res['data'] = $info;
        return $res;

    }

    public function upUserInfo(array $upData, array $where)
    {
        $res['success'] = false;
        $res['desc'] = '操作失败';
        $res['errorcode'] = '200022';
        if (empty($upData) || empty($where)) return $res;
        $ret = Db::table('kuaidi_user')->where($where)->update($upData);
        $res['success'] = true;
        $res['desc'] = '操作成功';
        $res['errorcode'] = '';
        return $res;
    }

    public function registerUser($data) {

        $where['open_id'] = ['=', $data['open_id']];
        $info = Db::table('kuaidi_user')->where($where)->find();
        $isUp  = false;
        if($info) {
            unset($data['open_id']);
            if(!empty($data)) {
                $isUp = true;
                Db::table('kuaidi_user')->where($where)->update($data);
            }
        }
        //只有手机号
        if(isset($data['tel'])) {
            $whereTel['tel'] = ['=', $data['tel']];
            $info = Db::table('kuaidi_user')->where($whereTel)->find();
            if($info && $isUp == false) {
                unset($data['tel']);
                unset($data['open_id']);
                if (!empty($data)) {
                    $isUp = true;
                    Db::table('kuaidi_user')->where($where)->update($data);
                }
            }
        }
        
        if(empty($info)) {
            if (empty($data['user_name'])) {
                $data['user_name'] = genUserName();
            }
            Db::table('kuaidi_user')->insert($data);
        }
        $infoDb = Db::table('kuaidi_user')->where($where)->find();
        if (empty($infoDb['avatar'])) {
            $infoDb['avatar'] = get_http_type() . $_SERVER['HTTP_HOST'] . "/image/icon.jpeg";
        } else {
            $infoDb['avatar'] = get_http_type() . $_SERVER['HTTP_HOST'] . $infoDb['avatar'];
        }
        $access_token = md5(uniqid() . mt_rand());

        $infoDb['token'] = $access_token;
        $infoDb['user_id'] = $infoDb['id'];
        Cache::set($access_token, $infoDb, 86400 * 10);
        $res['success'] = true;
        $res['desc'] = '操作成功';
        $res['data'] = $infoDb;
        $res['errorcode'] = '';
        return $res;
        
    }

    public function getUserInfoById($id) {
        $info = Db::table('kuaidi_user')->where('id', '=', $id)->find();
        if (empty($info['avatar'])) {
            $info['avatar'] = get_http_type() . $_SERVER['HTTP_HOST'] . "/image/icon.jpeg";
        } else {
            $info['avatar'] = get_http_type() . $_SERVER['HTTP_HOST'] . $info['avatar'];
        }
        $info['user_id'] = $info['id'];
        $res['success'] = true;
        $res['desc'] = '操作成功';
        $res['data'] = $info;
        $res['errorcode'] = '';
        return $res;
    }


}